package de.dhbw.karlsruhe.picsim.model.commands.impl;

import de.dhbw.karlsruhe.picsim.model.actions.ActionFactory;
import de.dhbw.karlsruhe.picsim.model.actions.PicAction;
import de.dhbw.karlsruhe.picsim.model.commands.PicInstruction;
import de.dhbw.karlsruhe.picsim.model.cpu.PicInstructionFacade;
import de.dhbw.karlsruhe.picsim.model.cpu.ReadOnlyFile;
import de.dhbw.karlsruhe.picsim.model.memory.RegisterValue;
import de.dhbw.karlsruhe.picsim.util.Bits;
import de.dhbw.karlsruhe.picsim.util.impl.BitUtilities;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: user
 * Date: May 3, 2010
 * Time: 1:04:49 AM
 */
abstract class RotateSupport implements PicInstruction {
    private final boolean targetIsWRegister;
    private final Bits argument;


    RotateSupport(final boolean targetIsWRegister, @NotNull final Bits argument) {
        this.targetIsWRegister = targetIsWRegister;
        this.argument = argument;
    }

    @NotNull
    @Override
    public final List<? extends PicAction> getActions(@NotNull final PicInstructionFacade pic) {
        return new InstructionSupport(targetIsWRegister, false, argument) {
            @NotNull
            @Override
            protected RegisterValue getValue(@NotNull final RegisterValue addressedFile,
                                             @NotNull final RegisterValue wRegister,
                                             @NotNull final List<PicAction> yourActions,
                                             @NotNull final ActionFactory actionFactory) {
                //Hohle das Status Register
                final ReadOnlyFile file = pic.getFor7BitAddress(BitUtilities.asLeastSignificantBitFirst(3, 7));

                //Übergebe das CarryFlag
                return doRotate(addressedFile, yourActions, actionFactory, file.getData().isSet(0));
            }
        }.getActions(pic);
    }

    @NotNull
    protected abstract RegisterValue doRotate(@NotNull RegisterValue addressedFile,
                                              @NotNull List<PicAction> yourActions,
                                              @NotNull ActionFactory actionFactory, boolean carryFlag);
}
